package com.cuz.for2024;

import java.util.HashMap;
import java.util.Map;

public class IT5 {
    public static void main(String[] args) {
        IT5 it5 = new IT5();
        System.out.println(it5.longestPalindrome("ac"));
    }
    public String longestPalindrome(String s) {
        char[] array = s.toCharArray();
        if (array.length == 0) {
            return "";
        }
        for (Map.Entry<Object, Object> objectObjectEntry : new HashMap<>().entrySet()) {

        }
        int max = 1;
        int start = 0;
        int end = 0;
        for (int i = 0; i < array.length; i++) {
            int[] temp1 = max(array, i, i + 1);
            int[] temp2 = max(array, i, i);
            if (temp1[0] > max) {
                max = temp1[0];
                start = temp1[1];
                end = temp1[2];
            }
            if (temp2[0] > max) {
                max = temp2[0];
                start = temp2[1];
                end = temp2[2];
            }
        }
        return s.substring(start, end+1);
    }

    // abccba
    // baabad
    private int[] max(char[] array, int i, int j) {
        while (i >= 0 && j < array.length) {
            if (array[i] == array[j]) {
                i--;
                j++;
            } else {
                break;
            }

        }
        //cbbd i=0 j=3
        int end = Math.min(j-1, array.length - 1);
        int start = Math.max(i+1, 0);
        return new int[]{end - start + 1, start, end};
    }
}
